//
//  VisitorView.m
//  新浪微博OC
//
//  Created by  justinchou on 16/2/26.
//  Copyright © 2016年  justinchou. All rights reserved.
//

#import "VisitorView.h"
#import <Masonry/Masonry.h>
#import "UIButton+Category.h"
#import "UILabel+Category.h"
#import "UIImageView+UIImageView.h"

@interface VisitorView ()
/// 动画图片
@property (nonatomic) UIImageView *iconView;
/// 遮罩图片
@property (nonatomic) UIImageView *maskIconView;
/// 界面主图片
@property (nonatomic) UIImageView *homeView;
/// 信息标签
@property (nonatomic) UILabel *messageLabel;
/// 注册按钮
@property (nonatomic) UIButton *registerButton;
/// 登录按钮
@property (nonatomic) UIButton *loginButton;

@end

@implementation VisitorView

#pragma mark - 懒加载控件
- (UIImageView *)iconView {
    if (_iconView == nil) {
        _iconView = [[UIImageView alloc] init];
        [_iconView setImage:[UIImage imageNamed:@"visitordiscover_feed_image_smallicon"]];
    }
    return _iconView;
}

- (UIImageView *)maskIconView {
    if (_maskIconView == nil) {
        _maskIconView = [[UIImageView alloc] init];
        [_maskIconView setImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
    }
    return _maskIconView;
}

- (UIImageView *)homeView {
    if (_homeView == nil) {
        _homeView = [[UIImageView alloc] initWithIamgeName:@"visitordiscover_feed_image_house"];
    }
    return _homeView;
}

- (UILabel *)messageLabel {
    if (_messageLabel == nil) {
        _messageLabel = [[UILabel alloc] initWithText:@"关注一些人，回这里看看有什么惊喜" TextColor:[UIColor darkGrayColor] FontSize:14];
    }
    
    [_messageLabel sizeToFit];
    
    return _messageLabel;
}

- (UIButton *)registerButton {
    if (_registerButton == nil) {
        _registerButton = [[UIButton alloc] initWithTitle:@"注册" TitleColor:[UIColor orangeColor] BackgroundImageName:@"common_button_white_disable"];
    }
    [_registerButton addTarget:self action:@selector(clickRegisterBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [_registerButton sizeToFit];
    
    return _registerButton;
}

- (UIButton *)loginButton {
    if (_loginButton == nil) {
        _loginButton = [[UIButton alloc] initWithTitle:@"登录" TitleColor:[UIColor darkGrayColor] BackgroundImageName:@"common_button_white_disable"];
    }
    [_loginButton addTarget:self action:@selector(clickLoginBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [_loginButton sizeToFit];
    
    return _loginButton;
}

#pragma mark - 监听方法
- (void)clickRegisterBtn {
    if ([self.delegate respondsToSelector:@selector(visitorViewDidRegister)]) {
        [self.delegate visitorViewDidRegister];
    }
}

- (void)clickLoginBtn {
    if ([self.delegate respondsToSelector:@selector(visitorViewDidLogin)]) {
        [self.delegate visitorViewDidLogin];
    }
}


#pragma mark - 构造函数
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor whiteColor];
    
    [self setupUI];
    
    [self startAnimation];
    
    return self;
}

#pragma mark - 设置界面
- (void)setupUI {
    //灰度设置
    self.backgroundColor = [UIColor colorWithWhite:(237.0/255.0) alpha:1.0];
    
    [self addSubview:self.iconView];
    [self addSubview:self.maskIconView];
    [self addSubview:self.homeView];
    [self addSubview:self.messageLabel];
    [self addSubview:self.registerButton];
    [self addSubview:self.loginButton];
    
    //Masonry自动布局
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self).offset(-50);
    }];
    
    [self.homeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.iconView);
    }];
    
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.iconView);
        make.top.equalTo(self.iconView.mas_bottom).offset(12);
        make.size.mas_equalTo(CGSizeMake(224, 36));
    }];
    
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.messageLabel);
        make.top.equalTo(self.messageLabel.mas_bottom).offset(12);
        //=============Masonry需要注意的地方！！！直接赋值 需要用mas_equalto===================================
        make.size.mas_equalTo(CGSizeMake(100, 36));
    }];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.messageLabel);
        make.top.equalTo(self.registerButton);
        make.size.mas_equalTo(CGSizeMake(100, 36));
    }];
    //遮罩视图
    [self.maskIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.top.equalTo(self);
        make.bottom.equalTo(self.registerButton.mas_bottom);
        
    }];
    //设置完控件，即可启动动画，不用让其它类来调用它
    [self startAnimation];
    
}

#pragma mark - 主界面旋转动画
- (void)startAnimation {
    
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    anim.toValue = @(2 * M_PI);
    anim.duration = 20;
    anim.repeatCount = MAXFLOAT;
    anim.removedOnCompletion = false;
    
    [self.iconView.layer addAnimation:anim forKey:nil];
}

- (void)setupViewInfoWithMessage:(NSString *)message HomeIconView:(NSString *)imageName {
    
    self.iconView.hidden = true;
    self.maskIconView.hidden = true;
    
    self.messageLabel.text = message;
    [self.homeView setImage:[UIImage imageNamed:imageName]];
}








@end
